#include "sys_clk.h"

void fnRTCDrive_Delay(void)
{
	u8 i;
	for (i = 200; i > 0; i--)
	{
		__NOP();
		__NOP();
		__NOP();
		__NOP();
		WDT->EN = 0xbb;
	}
}

void systick_init(u8 SYSCLK)
{
}
/**
	*********************************************************************************
	*
	*函数名称：
	*
	*函数参数：无
	*
	*函数返回值：无
	*
	*功能描述：
	*
	*********************************************************************************
**/
void syscfg(void)
{
	u8 ret = 1;
	ret = ret;
	SYSCTL->SYS_PS = 0x82;
	SYSCTL->SYS_PD = 0x0018;
	SYSCTL->MOD0_EN = 0xfff4;
	SYSCTL->MOD1_EN = 0x0fA0;
	SYSCTL->INTC_EN = 0x01ff;
	SYSCTL->KBI_EN = 0x0000;
	SYSCTL->SYS_PS = 0x00;

	/*延时*/
	fnRTCDrive_Delay();
	/*更换系统时钟，提升到高速模式*/
	ret = SysModeChg(2, 0);
	fnRTCDrive_Delay();
	fnRTCDrive_Delay();
	fnRTCDrive_Delay();
	fnRTCDrive_Delay();
	/*systick定时器*/
	SysTick_Config(73728);
}
void SystemDelay(u16 t)
{
	u16 i;
	while (t--)
	{
		for (i = 0; i < 400; i++)
			;
		WDT->EN = 0xbb;
	}
}

void fnWDT_Restart(void)
{
	WDT->EN = 0xbb; /* restart watch dog timer */
}

void fnDl645MainClock_Init(void)
{
	SYSCTL->SYS_PS = 0x82;
	SysModeChg(2, 1);
	SYSCTL->SYS_PD = 0x0018;
	SYSCTL->MOD0_EN = 0x3b54;
	SYSCTL->MOD1_EN = 0x0fe0;
	SYSCTL->INTC_EN = 0x01ff;
	SYSCTL->KBI_EN = 0x0000;
	SYSCTL->SYS_RST = 0x0000;
	SystemDelay(100);
	SYSCTL->SYS_PS = 0x00;
	return;
}

u32 fnDl645MainClock_Get(void)
{
	u32 m_freq = 1618496;
	if (((SYSCTL->OSC_CTRL1 >> 8) & 0x3) == 0x3)
		m_freq = 32768;
	else if (((SYSCTL->OSC_CTRL1 >> 8) & 0x7) == 0x2)
		m_freq = 1843200;
	else if (((SYSCTL->OSC_CTRL1 >> 8) & 0x7) == 0x1)
		m_freq = 3686400;
	else if (((SYSCTL->OSC_CTRL1 >> 8) & 0x7) == 0x0)
		m_freq = 7372800;
	else if (((SYSCTL->OSC_CTRL1 >> 8) & 0x7) == 0x4)
		m_freq = 14745600;
	else if (((SYSCTL->OSC_CTRL1 >> 8) & 0x7) == 0x5)
		m_freq = 29491200;
	if (SYSCTL->OSC_CTRL1 & 0x00000020)
		m_freq = 1843200 - 224704;
	return m_freq;
}
